package com.qfhd.soft.cjzcore.Core;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CJCore {

    public static void Toast(Context context, String content, boolean isLong) {
        Toast.makeText(context.getApplicationContext(), content, isLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show();
    }

    public static void ToastInMainThread(Activity activity, Context context, String content, boolean isLong) {
        activity.runOnUiThread(()->{
            Toast.makeText(context.getApplicationContext(), content, isLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show();
        });
    }

    public static String intToString(int number){
        return (""+number);
    }

    public static String floatToString(float number){
        return (""+number);
    }

    public static String getMd5(String str) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(
                    str.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }


        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10)
                hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));

        }

        return hex.toString();// 32位
//        return hex.toString().toString().substring(8, 24);// 16位
    }

    public static int getRealMoney_int(int money){
        return money/100;
    }

    public static float getRealMoney_float(float money){
        float tmp   = money/100;
        return tmp;
    }

    public static int getPercent(long downloadSize,long totalSize){
        float tmp   = downloadSize / (totalSize * 1.0f);
        return (int) (tmp*100);
    }

    public static void copyText(String text,Context context){
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", text);
// 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);

        Toast(context,"复制成功",false);
    }

    /**
     * @param str 需要拆分的数组
     * @param breakString 分隔符
     * @return 返回最后一块字符串
     */
    public static String getStringLastOne(String str,String breakString){
            String[] array = str.split(breakString);
            if(array.length > 0){
                String tmp = array[array.length-1];
                CJLogger.v("getStringLastOne:"+tmp);
                return tmp;
            }
            return "";
    }

    public static String getStringLastOne(String str){
        String[] array = str.split("/");
        if(array.length > 0){
            String tmp = array[array.length-1];
            CJLogger.v("getStringLastOne:"+tmp);
            return tmp;
        }
        return "";
    }
}
